package org.miyuki4j.cfgtool.app.gui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import lombok.extern.slf4j.Slf4j;
import org.miyuki4j.cfgtool.Commands;
import org.miyuki4j.cfgtool.ExceptionUtil;
import org.miyuki4j.cfgtool.IOUtils;
import org.miyuki4j.cfgtool.StringUtils;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/30/星期三 21:30
 * @Version 1.0
 */
@Slf4j
public class CodeGenerateGuiController {

    @FXML
    private TextField intputDirText;
    @FXML
    private TextField codeBaseDirText;
    @FXML
    private TextField codePackagePrefixText;
    @FXML
    private CheckBox useLomBokCheckBox;

    @FXML
    private TextArea responseText;

    @FXML
    public void codeGenerate(ActionEvent ae) {
        log.info("codeGenerate|actionEvent={}", ae);

        final String intputDirTextText = intputDirText.getText();
        final String codeBaseDirTextText = codeBaseDirText.getText();
        final String codePackagePrefixTextText = codePackagePrefixText.getText();
        final boolean useLomBokCheckBoxSelected = useLomBokCheckBox.isSelected();

        if (StringUtils.isBlank(intputDirTextText)){
            MainGui.showAlert("请选择输入目录");
            return;
        }
        if (!new File(intputDirTextText).exists()) {
            MainGui.showAlert("输入目录不存在");
            return;
        }
        if (StringUtils.isBlank(codeBaseDirTextText)) {
            MainGui.showAlert("请选择代码生成目录");
            return;
        }
        if (!new File(codeBaseDirTextText).exists()) {
            MainGui.showAlert("代码生成目录不存在");
            return;
        }
        if (StringUtils.isBlank(codePackagePrefixTextText)){
            MainGui.showAlert("请输入代码包前缀");
            return;
        }

        try {
            final String[] args =
                    {
                            "-generate_code",
                            "--encoding=UTF-8",
                            "--input_dir=" + intputDirTextText,
                            "--code_generate_dir=" + codeBaseDirTextText,
                            "--code_generate_package_prefix=" + codePackagePrefixTextText,
                            "--code_generator_type=freemarker",
                            "--code_generate_use_lombok=" + useLomBokCheckBoxSelected
                    };
            Commands.run(args);
            responseText.setText("生成成功");
        } catch (Exception e) {
            responseText.setText(ExceptionUtil.getStackTrace("生成失败", e));
        }
    }

    @FXML
    public void back(ActionEvent ae) {
        log.info("back|actionEvent={}", ae);
        MainGui.back();
    }

    public void chooseInputDir(ActionEvent ae) {
        log.info("chooseInputDir|actionEvent={}", ae);
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("选择一个文件夹");
        File selectedDirectory = directoryChooser.showDialog(MainGui.primaryStage);
        if (selectedDirectory != null) {
            log.info("chooseInputDir={}", selectedDirectory.getAbsolutePath());
            intputDirText.setText(selectedDirectory.getAbsolutePath());
        } else {
            MainGui.showAlert("未选择任何目录！");
        }
    }

    public void chooseCodeBaseDir(ActionEvent ae) {
        log.info("chooseCodeBaseDir|actionEvent={}", ae);
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("选择一个文件夹");
        File selectedDirectory = directoryChooser.showDialog(MainGui.primaryStage);
        if (selectedDirectory != null) {
            log.info("chooseCodeBaseDir={}", selectedDirectory.getAbsolutePath());
            codeBaseDirText.setText(selectedDirectory.getAbsolutePath());
        } else {
            MainGui.showAlert("未选择任何目录！");
        }
    }

}
